$(function() {
    $(document).on("click", ".layui-table-body table.layui-table tbody tr", function(e) {
        if ($(e.target).hasClass("layui-table-col-special") || $(e.target).parent().hasClass("layui-table-col-special")) {
            return false;
        }
        var index = $(this).attr('data-index'),
            tableBox = $(this).closest('.layui-table-box'),
            tableFixed = tableBox.find(".layui-table-fixed.layui-table-fixed-l"),
            tableBody = tableBox.find(".layui-table-body.layui-table-main"),
            tableDiv = tableFixed.length ? tableFixed : tableBody,
            checkCell = tableDiv.find("tr[data-index=" + index + "]").find("td div.laytable-cell-checkbox div.layui-form-checkbox i"),
            radioCell = tableDiv.find("tr[data-index=" + index + "]").find("td div.laytable-cell-radio div.layui-form-radio i");
        if (checkCell.length) {
            checkCell.click();
        }
        if (radioCell.length) {
            radioCell.click();
        }
    });
    $(document).on("click", "td div.laytable-cell-checkbox div.layui-form-checkbox,td div.laytable-cell-radio div.layui-form-radio", function(e) {
        e.stopPropagation();
    });
})


function addDefaultNode(type, dom) {
    var treeObj = $.fn.zTree.getZTreeObj(dom);
    var nodes = treeObj.getSelectedNodes()[0];
    var newNode = { id: "new", name: "新建节点", pId: nodes.pId };
    if (type == 'root') {
        newNode = treeObj.addNodes(null, newNode);
    } else if (type == 'same') {
        var parent = treeObj.getNodeByParam("id", nodes.pId, null);
        if (parent) {
            newNode = treeObj.addNodes(parent, newNode);
        } else {
            newNode = treeObj.addNodes(null, newNode);
        }
    } else if (type == 'next') {
        newNode = treeObj.addNodes(nodes, newNode);
    }

    var node = treeObj.getNodeByParam("id", 'new', null);
    treeObj.selectNode(node);
}



function addTab(title, url) {
    var jq = top.jQuery;
    var crumbsArr = []
    jq("#crumbs li").each(function() {
        crumbsArr.push(jq(this).find('h1').text())
    });

    for (var i = 0; i < crumbsArr.length; i++) {
        if (crumbsArr[i] == title) {
            jq("#crumbs li").eq(i).trigger('click')
            return false
        }
    }

    jq("#crumbs li").removeClass('active')
    var item = ''
    item += '<li class="active" data-url="'+url+'">'
    item += '    <h1>'+title+'</h1>'
    item += '    <i class="layui-icon">&#x1006;</i>'
    item += '</li>'
    jq("#crumbs").append(item)

    jq("#page iframe").hide()

    var page = ''
    page += '<iframe name="' + title + '" src="' + url + '"></iframe>'
    jq("#page").append(page)

    jq("#crumbs li i").click(function() {
        var text = $(this).parents('li').find('h1').text()
        jq('#page iframe').each(function() {
            var flag = jq(this).attr('name')
            if (flag == text) {
                jq(this).remove()
            }
        })

        jq(this).parents('li').remove()
        jq("#crumbs li").last().trigger('click')
    });


    jq("#crumbs li").click(function() {
        jq("#crumbs li").removeClass('active')
        jq("#page iframe").hide()
        jq(this).addClass('active')
        var text = jq(this).find('h1').text()

        jq("#page iframe").each(function() {
            var flag = jq(this).attr('name')
            if (flag == text) {
                jq(this).show()
                return false
            }
        });
    });

    var len = jq('#crumbs li').length
    var weight = window.screen.width
    console.log(len)
    if (weight <= 1024) {
        if (len == 5) {
            jq("#crumbs li").eq(3).find('i').trigger('click')
        }
    } else {
        if (len == 7) {
            jq("#crumbs li").eq(3).find('i').trigger('click')
        }
    }

}






function restoreTree(data, id, flag, dom, father) {
    if (id) {
        data.id = id
    }
    var flag = $("#" + flag).val()
    if (flag == 'root') {
        var treeObj = $.fn.zTree.getZTreeObj(dom);
        var newNode = data
        newNode = treeObj.addNodes(null, newNode);
        treeObj.selectNode(newNode[0]);

    } else if (flag == 'same' || flag == 'next') {
        var treeObj = $.fn.zTree.getZTreeObj(dom);
        var newNode = data
        newNode = treeObj.addNodes(father, newNode);
        treeObj.selectNode(newNode[0]);
    } else if (flag == 'edit') {
        var treeObj = $.fn.zTree.getZTreeObj(dom);
        var node = treeObj.getNodeByParam("id", data.id, null);
        var keyArr = []
        for (var key in data) {
            keyArr.push(key)
        }

        for (var i = 0; i < keyArr.length; i++) {
            node[keyArr[i]] = data[keyArr[i]]
        }
        treeObj.updateNode(node);
    }
    layer.closeAll();

    var treeObj = $.fn.zTree.getZTreeObj(dom);
    var node = treeObj.getNodeByParam("id", 'new', null);
    if (node) {
        treeObj.removeNode(node);
    }
}

function openBtn(dom) {
    for (var i = 0; i < dom.length; i++) {
        $("#" + dom[i]).removeClass('layui-btn-disabled')
        $("#" + dom[i]).attr('disabled', false)
    }
}

function closeBtn(dom) {
    for (var i = 0; i < dom.length; i++) {
        $("#" + dom[i]).addClass('layui-btn-disabled')
        $("#" + dom[i]).attr('disabled', true)
    }
}


function renderSelect(form, fun, req) {
    $('select').each(function(index) {
        var url = $(this).attr('data-url')
        console.log(url)
        if (url) {
            $.ajax({
                url: url,
                type: "POST",
                data: {},
                async: true,
                success: function(res) {
                    var data = res.data
                    var html = ''
                    for (var i = 0; i < data.length; i++) {
                        html += '<option value="' + data[i].id + '">' + data[i].name + '</option>'
                    }
                    $("select").eq(index).html(html)
                    form.render()

                    if (fun) {
                        if (req) {
                            fun(req)
                        } else {
                            fun()
                        }
                    }

                }
            });
        }
    })
}

function removeDefaultNode(dom, type) {
    var treeObj = $.fn.zTree.getZTreeObj(dom);
    var nodes = treeObj.getSelectedNodes()[0];
    var node = treeObj.getNodeByParam("id", nodes.pId, null);
    if (type) {
        if (nodes.name == '新建节点') {
            treeObj.selectNode(node);
            treeObj.removeNode(nodes);
        }
    }
}